package com.jack.thinking.java._22.swing.pane;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.utils.SwingConsole;

public class TicTacToe extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField rows = new JTextField("3"),
			cols = new JTextField("3");
	private enum State{BLANK, XX, OO}
	
	static class ToeDialog extends JDialog{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private State turn  = State.XX;
		ToeDialog(int  cellsW, int cellsH){
			setTitle("The game itself");
			setLayout(new GridLayout(cellsW, cellsH));
			for(int i = 0; i < cellsW * cellsH; i++) {
				add(new ToeButton());
			}
			setSize(cellsW * 50, cellsH * 50);
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		}
		
		class ToeButton extends JPanel{

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			private State state = State.BLANK;
			public ToeButton() {
				addMouseListener(new ML());
			}
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				int x1 =0, y1 = 0, x2 = getSize().width -1, y2 = getSize().height - 1;
				g.drawRect(x1, y1, x2, y2);        //画一个矩形
				x1 = x2 / 4;
				y1 = y2 / 4;
				int wide = x2 / 2, high = y2 / 2;
				if(state == State.XX) {
					g.drawLine(x1, y1, x1 + wide, y1 + wide);
					g.drawLine(x1, y1 + high, x1 + wide, y1);
				}
				
				if(state == State.OO) {
					g.drawOval(x1, y1, x1 + wide/2, y1 + high /2);
				}
			}
			
			class ML extends MouseAdapter{
				@Override
				public void mousePressed(MouseEvent e) {
					if(state == State.BLANK) {
						state = turn;
						turn = (turn == State.XX ? State.OO : State.XX);
					}else {
						state = (state == State.XX ? State.OO : State.XX);
					}
					repaint();
				}
			}
		}
	}
	
	class BL implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			JDialog d = new ToeDialog(new Integer(rows.getText()), new Integer(cols.getText()));
			d.setVisible(true); // 注意让他暴露出来
		}
	}
	
	public TicTacToe() {
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(2, 2));
		p.add(new JLabel("Rows", JLabel.CENTER));
		p.add(rows);
		p.add(new JLabel("Columns", JLabel.CENTER));
		p.add(cols);
		add(p, BorderLayout.NORTH);
		JButton b = new JButton("go");
		b.addActionListener(new BL());
		add(b, BorderLayout.SOUTH);
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new TicTacToe(), 200, 200);
	}
}
